package cn.gz.demo.ui;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import cn.gz.demo.R;
import cn.gz.demo.ScheduledExecutorServiceBinding;
import cn.gz.demo.ui.base.BaseActivity;

/**
 * @author Liuyz
 * @date 2020/6/1514:45
 */
public class ScheduledExecutorServiceActivity extends BaseActivity {
    private ScheduledExecutorServiceBinding binding;
    // 通过静态方法创建ScheduledExecutorService的实例
    private ScheduledExecutorService mScheduledExecutorService = Executors.newScheduledThreadPool(4);
    private Future future;
    private int number1 = 0;
    private int number2 = 0;
    private int number3 = 0;
    private int number4 = 0;
    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                binding.number1.setText(number1 + "");
            } else if (msg.what == 2) {
                binding.number2.setText(number2 + "");
            } else if (msg.what == 3) {
                binding.number3.setText(number3 + "");
            } else if (msg.what == 4) {
                binding.number4.setText(number4 + "");
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_scheduled_executor_service);

        // 延时任务
        mScheduledExecutorService.schedule(new Runnable() {
            @Override
            public void run() {
                ++number1;
                handler.sendEmptyMessage(1);
            }
        }, 1000, TimeUnit.MILLISECONDS);


        mScheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                ++number2;
                handler.sendEmptyMessage(2);
            }
        }, 1000, 1000, TimeUnit.MILLISECONDS);

        mScheduledExecutorService.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                ++number3;
                handler.sendEmptyMessage(3);
            }
        }, 1000, 1000, TimeUnit.MILLISECONDS);

        binding.start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                number4 = 0;
                start();
            }
        });
    }

    public void start() {
        if (future != null) {
            future.cancel(true);

        }
        // 循环任务，按照上一次任务的发起时间计算下一次任务的开始时间
        future = mScheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                ++number4;
                handler.sendEmptyMessage(4);
            }
        }, 1000, 1000, TimeUnit.MILLISECONDS);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
